home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / wais / waisgate / HTFWriter.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  5KB  |  220 lines

  1. /*        FILE WRITER                HTFWrite.h
  2. **        ===========
  3. **
  4. **    This version of the stream object just writes to a C file.
  5. **    The file is assumed open and left open.
  6. **
  7. **    Bugs:
  8. **        strings written must be less than buffer size.
  9. */
  10.  
  11. #include "HTFWriter.h"
  12.  
  13. #include "HTFormat.h"
  14. #include "HTAlert.h"
  15. #include "HTFile.h"
  16.  
  17. /*        Stream Object
  18. **        ------------
  19. */
  20.  
  21. struct _HTStream {
  22.     CONST HTStreamClass *    isa;
  23.     
  24.     FILE *            fp;
  25.     char *             end_command;
  26.     char *             remove_command;
  27. };
  28.  
  29.  
  30. /*_________________________________________________________________________
  31. **
  32. **            A C T I O N     R O U T I N E S
  33. **  Bug:
  34. **    All errors are ignored.
  35. */
  36.  
  37. /*    Character handling
  38. **    ------------------
  39. */
  40.  
  41. PRIVATE void HTFWriter_put_character ARGS2(HTStream *, me, char, c)
  42. {
  43.     putc(c, me->fp);
  44. }
  45.  
  46.  
  47.  
  48. /*    String handling
  49. **    ---------------
  50. **
  51. **    Strings must be smaller than this buffer size.
  52. */
  53. PRIVATE void HTFWriter_put_string ARGS2(HTStream *, me, CONST char*, s)
  54. {
  55.     fputs(s, me->fp);
  56. }
  57.  
  58.  
  59. /*    Buffer write.  Buffers can (and should!) be big.
  60. **    ------------
  61. */
  62. PRIVATE void HTFWriter_write ARGS3(HTStream *, me, CONST char*, s, int, l)
  63. {
  64.     fwrite(s, 1, l, me->fp); 
  65. }
  66.  
  67.  
  68.  
  69.  
  70. /*    Free an HTML object
  71. **    -------------------
  72. **
  73. **    Note that the SGML parsing context is freed, but the created
  74. **    object is not,
  75. **    as it takes on an existence of its own unless explicitly freed.
  76. */
  77. PRIVATE void HTFWriter_free ARGS1(HTStream *, me)
  78. {
  79.     fflush(me->fp);
  80.     if (me->end_command) {        /* Temp file */
  81.         fclose(me->fp);
  82.         HTProgress(me->end_command);    /* Tell user what's happening */
  83.     system(me->end_command);
  84.     free (me->end_command);
  85.     if (me->remove_command) {
  86.         system(me->remove_command);
  87.         free(me->remove_command);
  88.     }
  89.     }
  90.  
  91.     free(me);
  92. }
  93.  
  94. /*    End writing
  95. */
  96.  
  97. PRIVATE void HTFWriter_end_document ARGS1(HTStream *, me)
  98. {
  99.     fflush(me->fp);
  100. }
  101.  
  102.  
  103.  
  104. /*    Structured Object Class
  105. **    -----------------------
  106. */
  107. PUBLIC CONST HTStreamClass HTFWriter = /* As opposed to print etc */
  108. {        
  109.     "FileWriter",
  110.     HTFWriter_free,
  111.     HTFWriter_end_document,
  112.     HTFWriter_put_character,     HTFWriter_put_string,
  113.     HTFWriter_write
  114. }; 
  115.  
  116.  
  117. /*    Subclass-specific Methods
  118. **    -------------------------
  119. */
  120.  
  121. PUBLIC HTStream* HTFWriter_new ARGS1(FILE *, fp)
  122. {
  123.     HTStream* me;
  124.     
  125.     if (!fp) return NULL;
  126.  
  127.     me = (HTStream*)malloc(sizeof(*me));
  128.     if (me == NULL) outofmem(__FILE__, "HTML_new");
  129.     me->isa = &HTFWriter;       
  130.  
  131.     me->fp = fp;
  132.     me->end_command = NULL;
  133.     me->remove_command = NULL;
  134.  
  135.     return me;
  136. }
  137.  
  138. /*    Make system command from template
  139. **    ---------------------------------
  140. **
  141. **    See mailcap spec for description of template.
  142. */
  143. /* @@ to be written.  sprintfs will do for now.  */
  144.  
  145. /*    Take action using a system command
  146. **    ----------------------------------
  147. **
  148. **    originally from Ghostview handling by Marc Andreseen.
  149. **    Creates temporary file, writes to it, executes system command
  150. **    on end-document.  The suffix of the temp file can be given
  151. **    in case the application is fussy, or so that a generic opener can
  152. **    be used.
  153. */
  154. PUBLIC HTStream* HTSaveAndExecute ARGS3(
  155.     HTPresentation *,    pres,
  156.     HTParentAnchor *,    anchor,    /* Not used */
  157.     HTStream *,        sink)    /* Not used */
  158.  
  159. #ifdef unix
  160. #define REMOVE_COMMAND "/bin/rm -f %s\n"
  161. {
  162.     char *fnam;
  163.     CONST char * suffix;
  164.     
  165.     HTStream* me;
  166.     
  167.     me = (HTStream*)malloc(sizeof(*me));
  168.     if (me == NULL) outofmem(__FILE__, "Save and execute");
  169.     me->isa = &HTFWriter;  
  170.     
  171.     /* Save the file under a suitably suffixed name */
  172.     
  173.     suffix = HTFileSuffix(pres->rep);
  174.  
  175.     fnam = (char *)malloc (L_tmpnam + 16 + strlen(suffix));
  176.     tmpnam (fnam);
  177.     if (suffix) strcat(fnam, suffix);
  178.     
  179.     me->fp = fopen (fnam, "w");
  180.     if (!me->fp) {
  181.     HTAlert("Can't open temporary file!");
  182.         free(fnam);
  183.     free(me);
  184.     return NULL;
  185.     }
  186.  
  187. /*    Make command to process file
  188. */
  189.     me->end_command = (char *)malloc (
  190.                 (strlen (pres->command) + 10+ 3*strlen(fnam))
  191.                  * sizeof (char));
  192.     if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
  193.     
  194.     sprintf (me->end_command, pres->command, fnam, fnam, fnam);
  195.  
  196.     me->remove_command = NULL;    /* If needed, put into end_command */
  197. #ifdef NOPE
  198. /*    Make command to delete file
  199. */ 
  200.     me->remove_command = (char *)malloc (
  201.                 (strlen (REMOVE_COMMAND) + 10+ strlen(fnam))
  202.                  * sizeof (char));
  203.     if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
  204.     
  205.     sprintf (me->remove_command, REMOVE_COMMAND, fnam);
  206. #endif
  207.  
  208.     free (fnam);
  209.     return me;
  210. }
  211.  
  212. #else    /* Not unix */
  213. { return NULL; }
  214. #endif
  215.  
  216.  
  217. /*    Format Converter using system command
  218. **    -------------------------------------
  219. */
  220.